package com.example.mylibrarybase.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.example.mylibrarybase.utils.LogUtils;

/**
 * Created by 35876 于萌萌
 * 创建日期: 15:46 . 2016年11月07日
 * 描述:
 * <p>
 * <p>
 * 备注:
 */

public class MyViewPager extends ViewPager {

    private float downX;
    private float downY;

    public MyViewPager(Context context) {
        super(context);
    }

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }



    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                downX = ev.getX();
                downY = ev.getY();
                LogUtils.i("我点到了viewpager按下"+"x"+ downX +"\t"+"y"+ downY);
            break;
            case MotionEvent.ACTION_MOVE:
                float currentX = ev.getX();
                float currentY = ev.getY();
                if (Math.abs(currentX - downX) > Math.abs(currentY - downY)) {

                    if (currentX - downX > 0) {
                            LogUtils.i("向右移动");
                            getParent().requestDisallowInterceptTouchEvent(true);
                        //  mMotionTarget = findTarget(ev, ORIENTATION_RIGHT);
                    } else {
                        LogUtils.i("向左移动");
                            getParent().requestDisallowInterceptTouchEvent(true);
                        //  mMotionTarget = findTarget(ev, ORIENTATION_LEFT);
                    }

                } else {
                    if (currentY - downY > 0) {
                        LogUtils.i("向下移动");
                        if (currentY-downY>30){
                            getParent().requestDisallowInterceptTouchEvent(true);
                        }
                      //  mMotionTarget = findTarget(ev, ORIENTATION_DOWN);
                    } else {
                        LogUtils.i("向上移动");
                        if (currentY-downY<-30){
                            getParent().requestDisallowInterceptTouchEvent(true);
                        }
                       // mMotionTarget = findTarget(ev, ORIENTATION_UP);
                    }

                }
                 //只需这句话，让父类不拦截触摸事件就可以了。
                //LogUtils.i("我点到了viewpager移动"+"x"+ mX +"\t"+"y"+ mY +"\n"+"xm"+xm+"\t"+"ym"+ym);
            break;
            case MotionEvent.ACTION_UP:
                LogUtils.i("我点到了viewpager抬起");


                break;
        }
        return super.dispatchTouchEvent(ev);
    }
}
